package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.MemberService;
import com.itheima.service.OrderlistService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderlist")
public class OrderlistServlet {
    @Reference
    private OrderlistService orderlistService ;

    @Reference
    private MemberService memberService ;
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = orderlistService.findByPage(queryPageBean);
            return new Result(true,"查询页面成功", pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询页面失败");
        }
    };

    @RequestMapping("/add")
    public  Result add(@RequestBody Map<String,String> map ,Integer[]setmealId){
        try {
            Result result =  orderlistService.add(map,setmealId);
            return result ;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }
    @GetMapping("/findAll")
    public Result findAll(){
        List<Setmeal> all =  orderlistService.findAll();
        return new Result(true,"查询成功",all) ;
    }

    @DeleteMapping("/delete")
    public Result delete(Integer id){
        try {
            orderlistService.deleteById(id);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }

    }

    @RequestMapping("/changeStatus")
    public Result changeStatus(@RequestBody Map map){
        try {
            orderlistService.changeStatus(map);
            return new Result(true,"修改状态成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改状态失败");
        }
    }

    @GetMapping("/findById")
    public Result findById(Integer id){

        Map byId = orderlistService.findById(id);
        return new Result(true,"成功",byId);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Map map ,Integer[] setmealIds) throws Exception {
        System.out.println(map);
        return orderlistService.edit(map,setmealIds);

    }

}
